package com.zhouzi.zhaoyang.util;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Environment;

import com.zhouzi.zhaoyang.R;
import com.zhouzi.zhaoyang.bean.VersionCodeBean;

import org.jetbrains.annotations.NotNull;

import constacne.UiType;
import listener.UpdateDownloadListener;
import model.UiConfig;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class DownLoadApkUtil {

    private static UiConfig uiConfig;
    private static UpdateConfig updateConfig;

    public static String getVersionName(Activity activity) {
        PackageManager packageManager = activity.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    activity.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "-1";
    }


    public static void update(VersionCodeBean data) {

        UpdateAppUtils.getInstance().deleteInstalledApk();

        uiConfig = new UiConfig();
        uiConfig.setUiType(UiType.PLENTIFUL);
        uiConfig.setCancelBtnText("下次再说");
        uiConfig.setUpdateLogoImgRes(R.drawable.ic_update_logo);
        uiConfig.setUpdateBtnBgRes(R.drawable.bg_update_btn);
        uiConfig.setTitleTextColor(Color.BLACK);
        uiConfig.setTitleTextSize(18f);
        uiConfig.setContentTextColor(Color.parseColor("#88e16531"));


        if (  data != null && data.isMust() != null &&  data.isMust() == 0 && data.getDownUrl() != null && data.getDescription() != null ) {
            UpdateAppUtils
                    .getInstance()
                    .apkUrl(data.getDownUrl())
                    .updateTitle("版本更新")
                    .uiConfig(uiConfig)
                    .updateContent(data.getDescription())
                    .update();

        } else {

            updateConfig = new UpdateConfig();
            updateConfig.setForce(true);
            updateConfig.isDebug();
//            updateConfig.setCheckWifi(true);
            updateConfig.isShowNotification();
            updateConfig.setNotifyImgRes(R.drawable.ic_update_logo);
            updateConfig.setApkSavePath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZhaoYang");
            updateConfig.setApkSaveName("ZhaoYang");


            if ( data != null && data.getDownUrl() != null && data.getDescription() != null){
                UpdateAppUtils
                        .getInstance()
                        .apkUrl(data.getDownUrl())
                        .updateTitle("版本更新")
                        .updateContent(data.getDescription())
                        .updateConfig(updateConfig)
                        .uiConfig(uiConfig)
                        .setUpdateDownloadListener(new UpdateDownloadListener() {
                            @Override
                            public void onStart() {

                            }

                            @Override
                            public void onDownload(int i) {

                            }

                            @Override
                            public void onFinish() {

                            }

                            @Override
                            public void onError(@NotNull Throwable throwable) {

                            }
                        })
                        .update();
            }


        }

    }



}
